const { getAll, getOne } = require('../../utils/dbTools');
// 数据表名称
const tableName = 'business';

/**
 * 获取医疗机构列表
 * 
*/
const businessList = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { page, size } = req.query;
    const params = {};
    if (page && size) {
        params.page = page;
        params.size = size;
    }
    const result = await getAll(tableName, params);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    output.data = result;
    res.send(output);
}

/**
 * 获取医疗机构信息
 * 
*/
const businessInfo = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { id } = req.query;
    if (!id) {
        output.code = 500;
        output.msg = "参数错误: 缺少参数id";
        return res.send(output);
    }
    const info = await getOne(tableName, { id });
    if (info) {
        output.data = info;
    } else {
        output.code = 404;
        output.msg = "商户id错误: 未查询到相关数据";
    }
    res.send(output);
}

/**
 * 获取医生列表
 * 
*/
const doctorList = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { bus_id, page, size } = req.query;
    const params = {};
    if (page && size) {
        params.page = page;
        params.size = size;
    }
    if (!bus_id) {
        output.code = 500;
        output.msg = "参数错误: 缺少参数bus_id";
        return res.send(output);
    }
    params.bus_id = bus_id;
    const result = await getAll(tableName, params);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    output.data = result;
    res.send(output);
}


module.exports = {
    businessList,
    doctorList,
    businessInfo
}